<?php
/**
 * Request Method
 * 
 * @package util
 * @author wangzhiliang
 *
 */
class Request
{
    /**
     * 获取URL 内容
     * 正常返回内容，出错返回错误信息
     * 
     * @param string $url	//url
     * @param string $message 出错信息
     * @param string $protocol  //http||https
     * @return mix $content
     */
    public static function getContent ($url, &$message, $protocol = 'http')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if ($protocol === 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        }
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $content = curl_exec($ch);
        if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
            $message = curl_getinfo($ch);
            curl_close($ch);
            return FALSE;
        }
        curl_close($ch);
        return $content;
    }
    /**
     * 获取xml内容
     *
     * @param string $url
     * @param string $message 
     * @param string $protocol
     * @return SimpleXMLElement
     */
    public static function getXML ($url, &$message, $protocol = 'http')
    {
        $content = self::getContent($url, $message, $protocol);
        if ($content === FALSE) {
            return FALSE;
        }
        $xml = @simplexml_load_string(trim($content));
        if (! ($xml instanceof SimpleXMLElement)) {
            $message = $url . '<br />xml 结构错误';
            return FALSE;
        }
        return $xml;
    }
}